package sfsoftware.game.actor;

import sfsoftware.game.TrevorLegacy;
import sfsoftware.game.actor.attributes.Collidable;
import sfsoftware.util.*;

public class ScriptedActor extends Actor implements Updatable {
	private String script;	// res/scripts/bleh.trv
	private int destX;
	private int destY;
	
	public ScriptedActor(int destX, int destY, String texture, int destWidth, int destHeight, int srcX, int srcY, String scriptFile) {
		super(destX, destY, texture, srcX, srcY, destWidth, destHeight);
		
		script = scriptFile;					
		this.destX = destX;
		this.destY = destY;		
		
		setSpeed(4);
		setWalkable(false);		
	}

	/**
	 * Returns a boolean indicating if the Actor is at their destination location
	 * 
	 * @return
	 */
	public boolean atDestLocation()
	{
		if(destX != getX() || destY != getY())
		{
			//System.out.println("Distance to point: " + distanceToPoint(destX + (getWidth() / 2), destY + (getHeight() / 2)));
			if(distanceToPoint(destX + (getWidth() / 2), destY + (getHeight() / 2)) <= getSpeed())
			{
				setSpeed(getSpeed() - 1);
				//System.out.println(getSpeed());
			}
			else
			{
				setSpeed(4);			
			}
		}
		
		
		return (destX == getX() && destY == getY());		
	}		
	
	@Override
	public void collisionResult(Collidable object) {
		super.collisionResult(object);
		// Scripts are only triggered if it's Trevor bumping us.
		if(object.equals(TrevorLegacy.player))
		{			
			runAssociatedScript();
		}
	}	
	
	// Gets the angle in radians from the current position to the destination postition.
	private double getAngleToDest()
	{
		double dx = (getX() + (getWidth() / 2)) - (destX + (getWidth() / 2));
		double dy = (getY() + (getHeight() / 2)) - (destY + (getHeight() / 2));
		double inRads = Math.atan2(dy,dx);
		
		if (inRads < 0)
		{
	        inRads = Math.abs(inRads);
		}
	    else
	    {
	        inRads = 2 * Math.PI - inRads;
	    }			
		
		return inRads;
	}

	protected void runAssociatedScript()
	{
		// Only kick off the script if it isn't already running
		if(!(Script.isScriptRunning(script)))
		{
			// TODO Technically if this runs twice it is a bug as the collision result should only be called once.
			new Script(script, this);
		}
	}
	
	/**
	 * Sets the destination X coordinate to the provided value
	 * 
	 * @param newDestX
	 */
	public void setDestX(int newDestX)
	{
		destX = newDestX;
	}
	
	/**
	 * Sets the destination Y coordinate to the provided value
	 * 
	 * @param newDestY
	 */
	public void setDestY(int newDestY)
	{
		destY = newDestY;
	}
	
	/**
	 * If the actor's current location is not the same as the destination, move it! 
	 */
	@Override
	public void update() 
	{
		if(!(atDestLocation()))
		{
			double xDirection = Math.cos(getAngleToDest()) * -1;
			double yDirection = Math.sin(getAngleToDest());		
			
			try
			{
				move(xDirection, yDirection, TrevorLegacy.tileMap);
			}
			catch(ArrayIndexOutOfBoundsException e)
			{

			}
		}		
	}
	
}
